require'net/http'
require'json'
module JavaEye
  AUTH_URL="http://api.javaeye.com/api/auth/verify"
  VIA="EyeShoes"
  LIST_URL="http://api.javaeye.com/api/twitters/list"
  REPLY_URL='http://api.javaeye.com/api/twitters/replies'
  ALL_URL='http://api.javaeye.com/api/twitters/all'
  CREATE_URL="http://api.javaeye.com/api/twitters/create"
  DELETE_URL="http://api.javaeye.com/api/twitters/destroy"
  SHOW_URL="http://api.javaeye.com/api/twitters/show"
  class User
    %W[name domain id logo].each do |attr|
      attr_accessor attr.to_sym
    end
    def initialize(hash)
      hash.each do |key,value|
        self.send("#{key}=",value)
      end
    end
  end
  class Twitter
    %W[user id created_at body name logo domain receiver reply_to_id via].each do |attr|
      attr_accessor attr.to_sym
    end
    def initialize(hash)
      hash.each do |key,value|
        if key=="user"
          self.send("#{key}=",User.new(value))
        else
        self.send("#{key}=",value)
        end
      end
    end
  end
  class Client
    attr_accessor:user
    attr_accessor:auth_code
    def initialize(username,password)
      @username=username
      @password=password
    end
    def auth
        resp=basic_get_request(AUTH_URL)
        case resp.code
        when "200"
          @auth_code="200"
          @user=User.new(JSON.parse(resp.body))
        when "400"
          @auth_code="400"
        when "401"
          @auth_code="401"
        end
    end
    def option2query(option)
       query=''
       option.each do |key,value|
        query<<(key.to_s+'='+value.to_s+'&')
      end
      query.chop
    end
    def get_request_uri(path,option)
      query=option2query(option)
      if query.empty?
        path
      else
        path+'?'+query
      end
    end
    def basic_get_request(url,option={})
      url=get_request_uri(url,option)
      url=URI.parse(url)
      Net::HTTP.start(url.host) do |http|
        req=Net::HTTP::Get.new(url.request_uri)
        req.basic_auth @username,@password
        resp=http.request(req)
        return resp
      end
    end
    def basic_post_request(url,data,option={})
      url=get_request_uri(url,option)
      url=URI.parse(url)
      Net::HTTP.start(url.host) do |http|
        req=Net::HTTP::Post.new(url.request_uri)
        req.basic_auth @username,@password
        req.set_form_data(data)
        resp=http.request(req)
        return resp
      end
    end
    def get_twitters(url,option)
      resp=basic_get_request(url,option)
      case resp.code
        when "200"
          @auth_code="200"
          twitters||=[]
          JSON.parse(resp.body).each do |twitter|
            twitters<<Twitter.new(twitter)
          end
          twitters
        when "400"
          @auth_code="400"
          nil
        when "401"
          @auth_code="401"
          nil
      end
    end
    def get_list(option={})
      get_twitters(LIST_URL,option)
    end
    def get_reply(option={})
      get_twitters(REPLY_URL,option)
    end
    def get_all(option={})
      get_twitters(ALL_URL,option)
    end
    def create(data)
      data['via']=VIA
      resp=basic_post_request(CREATE_URL,data)
      if resp.code=='200'
        true
      else
        false
      end
    end
    def delete(id)
      data={}
      data["id"]=id.to_s
      resp=basic_post_request(DELETE_URL,data)
      if resp.code=='200'
        true
      else
        false
      end
    end
    def show(id)
      data={}
      data["id"]=id.to_s
      resp=basic_post_reqest(SHOW_URL,data)
      if resp.code=='200'
        twitters=[]
        (JSON.parse resp.body).to_a.each do |twitter|
          twitters<<Twitter.new(twitter)
        end
        twitters
      else
        nil
      end
      
    end
  end
end